package cn.foolishbird.hamal.frame.bsassert;

import cn.foolishbird.hamal.frame.exception.BusinessException;
import cn.foolishbird.hamal.frame.exception.base.BaseException;
import cn.foolishbird.hamal.frame.bsassert.condition.Condition;
import cn.foolishbird.hamal.frame.bsassert.condition.NotNullCondition;
import cn.foolishbird.hamal.frame.bsassert.info.RstInfo;
import lombok.extern.slf4j.Slf4j;

/**
 * 业务断言工具类
 *
 * @author Eric
 * @version 1.0
 * @date 2019-12-13
 */
@Slf4j
public class BsAssert {

    /**
     * 例子方法，空值判断
     *
     * @param obj  1
     * @param info 2
     * @return void
     * @throws
     * @author Eric
     * @date 2019/12/13
     **/
    public static void nullAssert(Object obj, RstInfo info) throws BaseException {
        bsAssert(new NotNullCondition<Object>(obj), info, BusinessException.class);
    }

    /**
     * 条件接口 基础方法，适用于一些条件判断较复杂的
     * 实现条件接口后，缩小业务部分代码
     *
     * @param condition 条件接口
     * @param info      错误信息
     * @param exception 错误类型
     * @return void
     * @throws
     * @author Eric
     * @date 2019/12/13
     **/
    public static <T extends BaseException> void bsAssert(Condition condition, RstInfo info, Class<T> exception) throws BaseException {
        bsAssert(condition.condition(), info);
    }

    /**
     * @param info 2
     * @return void
     * @throws
     * @author Eric
     * @date 2019/12/13
     **/
    public static <T extends BaseException> void bsAssert(RstInfo info) throws BaseException {
        bsAssert(true, info);
    }

    /**
     * 断言基础方法
     *
     * @param condition 条件
     * @param info      错误信息
     * @return void
     * @throws
     * @author Eric
     * @date 2019/12/13
     **/
    public static <T extends BaseException> void bsAssert(boolean condition, RstInfo info) {
        if (condition) {
            throw new BusinessException(info.code(), info.content(), info.data());
        }
    }
}
